/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2018，所有权利保留。
 * 
 * 项目名：	demo
 * 文件名：	WebConfiguration.java
 * 模块说明：	
 * 修改历史：
 * 2018年2月24日 - wangdanhua - 创建。
 */
package xyz.wangdanhua.demo.config;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.filters.RemoteIpFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wangdanhua
 *
 */
@Configuration
public class WebConfiguration {

  @Bean
  public RemoteIpFilter remoteIpFilter() {
    return new RemoteIpFilter();
  }
  
  @Bean
  public MyFilter myFilter() {
    return new MyFilter();
  }

//  @Bean
//  public FilterRegistrationBean testFilterRegistration() {
//
//    FilterRegistrationBean registration = new FilterRegistrationBean();
//    registration.setFilter(new MyFilter());
//    registration.addUrlPatterns("/*");
//    registration.addInitParameter("paramName", "paramValue");
//    registration.setName("MyFilter");
//    registration.setOrder(1);
//    return registration;
//  }

  public class MyFilter implements Filter {
    @Override
    public void destroy() {
      // TODO Auto-generated method stub
    }

    @Override
    public void doFilter(ServletRequest srequest, ServletResponse sresponse,
        FilterChain filterChain) throws IOException, ServletException {
      // TODO Auto-generated method stub
      HttpServletRequest request = (HttpServletRequest) srequest;
      System.out.println("this is MyFilter,url :" + request.getRequestURI());
      filterChain.doFilter(srequest, sresponse);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
      // TODO Auto-generated method stub
    }
  }
}
